#include <bits/stdc++.h>
// 2025/02/06
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

struct garbage{
    int t, f, h;
} gar[101];

int d, g, dp[101][1001];

signed main()
{
    cin >> d >> g;
    for (int i = 1; i <= g; i++)
        cin >> gar[i].t >> gar[i].f >> gar[i].h;
    sort(gar + 1, gar + g + 1, [](garbage& a, garbage& b) { return a.t < b.t; });
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 10, gar[0].t = 0, gar[0].f = 0, gar[0].h = 0;
    for (int i = 0; i < g; i++) {
        for (int j = 0; j <= d; j++) {
            if (dp[i][j] < 0)
                continue;
            if (j + gar[i + 1].h >= d && dp[i][j] >= gar[i + 1].t - gar[i].t)
                return cout << gar[i + 1].t, 0;
            if (dp[i][j] - gar[i + 1].t + gar[i].t >= 0){
                dp[i + 1][j + gar[i + 1].h] = dp[i][j] - gar[i + 1].t + gar[i].t;
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - gar[i + 1].t + gar[i].t + gar[i + 1].f);
            }
        }
    }
    int m = 10, s = 0;
    for (int i = 1; i <= g; i++){
        if (gar[i].t - gar[i - 1].t > m)
            cout << s + m << endl, exit(0);
        s += gar[i].t - gar[i - 1].t;
        m -= gar[i].t - gar[i - 1].t;
        m += gar[i].f;
    }
    cout << s + m << endl;
    return 0;
}